package com.study.springboot.SpringBoot_A.shiro;

import com.study.springboot.SpringBoot_A.bean.SysPermission;
import com.study.springboot.SpringBoot_A.bean.SysRole;
import com.study.springboot.SpringBoot_A.bean.UserInfo;
import com.study.springboot.SpringBoot_A.service.UserInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 赵超
 * @Description
 * @create 2018-03-31 15:19
 **/
public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserInfoService userInfoService;

    public MyShiroRealm() {
//        System.out.println("userInfoService  " + userInfoService);
    }

    /**
     * 链接权限的实现shiro的权限授权是通过继承AuthorizingRealm抽象类，重载doGetAuthorizationInfo();
     * 当访问到页面的时候，链接配置了相应的权限或者shiro标签才会执行此方法否则不会执行，
     * 所以如果只是简单的身份认证没有权限的控制的话，那么这个方法可以不进行实现，直接返回null即可。
     * 在这个方法中主要是使用类：SimpleAuthorizationInfo进行角色的添加和权限的添加。
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("权限配置-->MyShiroRealm.doGetAuthorizationInfo()");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        UserInfo userInfo = (UserInfo) principalCollection.getPrimaryPrincipal();
        for (SysRole role : userInfo.getRoleList()) {
            authorizationInfo.addRole(role.getRole());
            for (SysPermission p : role.getPermissions()) {
                authorizationInfo.addStringPermission(p.getPermission());
                /*
                    当然也可以添加set集合：
                        roles是从数据库查询的当前用户的角色，
                        stringPermissions是从数据库查询的当前用户对应的权限
                    authorizationInfo.setRoles(roles);
                    authorizationInfo.setStringPermissions(stringPermissions);
                 */
            }
        }
        return authorizationInfo;
    }

    /**
     * 重写获取用户信息的方法
     *
     * @param authenticationToken
     * @return
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("MyShiroRealm.doGetAuthenticationInfo");
        //获取用户输入的账号
        String username = (String) authenticationToken.getPrincipal();
        System.out.println(authenticationToken.getCredentials());
        //通过username在数据库中查找UserInfo对象
        //这里可以根据实际情况做缓存，如果不做，Shiro自己也是有时间间隔机制，2分钟内不会重复执行该方法
        UserInfo userInfo = userInfoService.findByUsername(username);
        System.out.println("userInfo: " + userInfo);
        if (null == userInfo) {
            return null;
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                userInfo,//用户名
                userInfo.getPassword(),//密码
//                ByteSource.Util.bytes(userInfo.getCredentialsSalt()),//salt=username+salt
                ByteSource.Util.bytes(userInfo.getSalt()),//salt=username+salt
                getName());
        SecurityUtils.getSubject().getSession().setAttribute("login", userInfo);
        return simpleAuthenticationInfo;
    }
}
